<?php
/**
 * 《已读信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CustomerUtil.class.php');

class Read extends CustomerUtil {
	public $tbl = Constant::tbl_enquiry_read;

	public $status = array(
		'-' => '请选择',
		'1' => '未发送',
		'2' => '已发送',
		
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'email' => '收件人',
		'emp' => '业务员',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'customer/read/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('123')) {
			return '';
		}

		return " emp = '" . $_SESSION['id'] . "'";
	}

	function _where($request) {
		$id_s = $request['id_s'];
		$to_s = $request['to_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($id_s)) {
			$where .= " AND enquiry = '" . $id_s . "'";
		}

		if (!empty($to_s)) {
			$where .= " AND `to` = '" . $to_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'customer/read/list');

		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		/*
		 if ($row['emp'] == $_SESSION['id']) {
			return true;
			}*/

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/read/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$template = $this->id(Constant::tbl_enquiry_template, $row['template']);
		$row['subject'] = $template['subject'];
		$row['content'] = $template['content'];

		$row = $this->_view_t($row);
		
		return $row;
	}

	function ajax_img($controller, $request) {
		$id = $request['id'];

		if ($row = $this->id(Constant::tbl_enquiry, $id)) {
			$template = $row['template'];

			$db = newdb($this->dsn_w);
			$fmt = "INSERT INTO %s(enquiry, `to`, template, ip, ua, emp, createtime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now())";
			$sql = sprintf($fmt, $this->tbl, $id, $row['to'], $template, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'], $row['emp']);
			debug($sql);
			$db->query($sql);
		}

		header ("Content-type: image/png");
		// $im = imagecreate(300, 100);
		$im = imagecreate(1, 1);
		$background_color = imagecolorallocate($im, 255, 255, 255);
		$text_color = imagecolorallocate($im, 233, 14, 91);
		imagestring($im, 2, 5, 5,  "www.tecmold.com", $text_color);
		imagepng($im);
		ImageDestroy($im);
	}
	
function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '发送邮件已读情况';

		$this->result_s = 20;
		$this->groupby_s = 'emp';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	function t($controller, $request) {
		$file = "/home/sunxy/a.txt";

		$s = file_get_contents($file);

		echo base64_decode($s);

		logdate($controller, $request);
		return true;
	}
}
?>
